package com.example.demo;

import javax.swing.*;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;

/**
 * 类描述信息
 *
 * @author TongDaShuai
 * @since 2021/11/10
 */
public class DrawPanel extends JFrame {
    public static void main(String args[]){
        GraphicsDemo myGraphicsFrame = new GraphicsDemo();
        myGraphicsFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
class ShapesPanel extends JPanel{
    ShapesPanel(){
        setBackground(Color.white);
    }
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        setBackground(Color.red); //背景色为黄色
//        g.setXORMode(Color.red); //设置XOR绘图模式,颜色为红色
//        g.setColor(Color.green);
//        g.fillRect(20, 20, 80, 40); //实际颜色是green + yellow的混合色=灰色
//        g.setColor(Color.yellow);
//        g.fillRect(60, 20, 80, 40); //后一半是yellow+yellow=read,前一半是yellow+灰色
//        g.setColor(Color.green);
//        g.fillRect(20, 70, 80, 40); //实际颜色是green+yellow的混合色=灰色.
//        g.fillRect(60, 70, 80, 40);
//        //前一半是(green+yellow)+gray =背景色,后一半是green+yellow = gray
//        g.setColor(Color.green);
//        g.drawLine(80, 100, 180, 200); //该直线是green+yellow = gray
//        g.drawLine(100, 100, 200, 200); //同上
//        /*再绘制部分重叠的直线.原直线中间段是灰色+灰色=背景色,延长部分是green+yellow=gray.*/
//        g.drawLine(140, 140, 220, 220);
//        g.setColor(Color.yellow); //分析下列直线颜色变化,与早先的力有重叠
//        g.drawLine(20, 30, 160, 30);
//        g.drawLine(20, 75, 160, 75);
//        g.setFont(new Font("黑体", Font.PLAIN, 20));
//
//        g.drawString("hello world", 30, 30);


        Font f = new Font("微软雅黑", Font.BOLD, 23);
//        g.setFont(f);
//        g.drawString("hello world", 30, 30);
//        Font f2 = new Font("微软雅黑", Font.PLAIN, 23);
//        g.setFont(f2);
//        g.drawString("hello world", 30, 30);

//        GlyphVector v = f.createGlyphVector(getFontMetrics(f).getFontRenderContext(), "Hello");
//        Shape shape = v.getOutline();
//
//        Rectangle bounds = shape.getBounds();
//
        Graphics2D g2 = (Graphics2D) g;
////        g2.translate(
////                (getWidth() - bounds.width) / 2 - bounds.x,
////                (getHeight() - bounds.height) / 2 - bounds.y
////        );
//        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//
//        g2.setColor(Color.WHITE);
//        g2.setStroke(new BasicStroke(3));
//        g2.draw(shape);
//        g2.setColor(Color.BLACK);
//        g2.fill(shape);

//        AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f);
//        g2.setComposite(ac);

        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
        g2.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
        FontRenderContext frc = g2.getFontRenderContext();
        TextLayout tl = new TextLayout("网址：", new Font("微软雅黑", Font.PLAIN,13), frc);
        Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(0,13));
        g2.setStroke(new BasicStroke(3.0f));
        g2.setColor(Color.WHITE);
        g2.draw(sha);
        g2.setColor(Color.BLACK);
        g2.fill(sha);
        g2.dispose();
    }
}
class GraphicsDemo extends JFrame{
    public GraphicsDemo(){
        this.getContentPane().add(new ShapesPanel());
        setTitle("基本绘图方法演示");
        setSize(300, 300);
        setVisible(true);
    }
}